package leader.action;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;

import teacher.bean.Teacher;

import admin.service.member.LeaderServiceAdmin;


public class MemberCount {
	
	List<Map> lists0=new ArrayList<Map>();
	private int direct;
	public int getDirect() {
		return direct;
	}


	public void setDirect(int direct) {
		this.direct = direct;
	}


	public List<Map> getLists0() {
		return lists0;
	}


	public void setLists0(List<Map> lists0) {
		this.lists0 = lists0;
	}


	public List<Map> getLists02() {
		return lists02;
	}


	public void setLists02(List<Map> lists02) {
		this.lists02 = lists02;
	}


	public List<Map> getLists03() {
		return lists03;
	}


	public void setLists03(List<Map> lists03) {
		this.lists03 = lists03;
	}


	List<Map> lists02=new ArrayList<Map>();
	List<Map> lists03=new ArrayList<Map>();

	
	public String execute()
	{
		LeaderServiceAdmin ls=new LeaderServiceAdmin();
		Teacher tea=(Teacher)ActionContext.getContext().getSession().get("leader");
		lists0=ls.getDirectionName(tea);
		setDirect(tea.getDirection());
		
		String a[] = new String[lists0.size()];	
		for(int i=0;i<lists0.size();i++)
		{			
			Map map=lists0.get(i);					
			a[i]=(String) map.get(1);		
		}
		Arrays.sort(a, Collator.getInstance(java.util.Locale.CHINA));//升序; 		
		System.out.println(Arrays.toString(a)); 
		
		for(int i=0;i<lists0.size();i++)
		{	
			
			try {
				if(lists0.get(i).get(14).equals("1"))
				{					
					Map map4=new HashMap();	
					map4.put(1, lists0.get(i).get(1)+"(组长)");
					map4.put(2, lists0.get(i).get(2));
					map4.put(3, lists0.get(i).get(0));
					
					lists02.add(map4);				
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}	
			int p=0;			
			Map mapx0=new HashMap();
			Map mapy0=new HashMap();
			Map mapz0=new HashMap();
			
			for(int i=0;i<a.length;i++)
			{
				
				int flag=0;
				
				for(int j=0;j<lists02.size();j++)
				{
					if(!(lists02.get(j).get(1).toString().contains(a[i])))
						flag++;										
				}

				if(lists02.size()==flag)			
				{
						
						Teacher tea3=new Teacher();
						tea3.setName(a[i]);
						lists03=ls.getUserFromName(tea3);	
					if(lists03.size()!=1)
					{		
						//System.out.println("我是老贺");
						for(int k=0;k<lists03.size();k++)
						{
							Map map3=lists03.get(k);
							mapx0.put(p, map3.get(1));
							mapy0.put(p, map3.get(2));						
							mapz0.put(p, map3.get(0));						
													
							p++;
							i++;
						}											
					}

					else
						{
							Map map3=lists03.get(0);	
							mapx0.put(p, a[i]);
							mapy0.put(p, map3.get(2));	
							mapz0.put(p, map3.get(0));	
							
							p++;
						}				
				}
				else
				{
					Teacher tea4=new Teacher();
					tea4.setName(a[i]);
					lists03=ls.getUserFromName(tea4);	
				if(lists03.size()!=1)
				{	for(int k=0;k<lists03.size();k++)
					{
						i++;
						Map map3=lists03.get(k);
						try {
							if(!map3.get(14).equals("1"))
							{	
								System.out.println("老贺的权限值"+map3.get(14));
								mapx0.put(p, map3.get(1));
								mapy0.put(p, map3.get(2));
								mapz0.put(p, map3.get(0));
								
								p++;
								
							}
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						//	i=i+lists03.size()-lists02.size();
					}
				i=i-1;
				}					
				}
			}	
			for(int a1=0;a1<mapx0.size();a1++)
			{
				Map map=new HashMap();				
				map.put(1,mapx0.get(a1));
				map.put(2,mapy0.get(a1));					
				map.put(3,mapz0.get(a1));					
				
				lists02.add(map);
			}
		return "succ";
	}
}
